package com.rt.code.controller;

import com.google.gson.Gson;
import com.rt.code.dto.A1templatecolumnDto;
import com.rt.code.dto.A1templatetableDto;
import com.rt.code.dto.PageDto;
import com.rt.code.service.intf.IA1templatecolumnService;
import com.rt.code.service.intf.IA1templatetableService;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.utils.StringUtils;
import com.rt.tmpt.utils.WebUtils;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@ApiIgnore
@Controller
@RequestMapping("/admin123/coding/a1templatecolumn")
public class A1templatecolumnAction {

	@Resource
	private IA1templatecolumnService a1templatecolumnService;
	@Resource
	private IA1templatetableService templatetableService;

	/**
	* 分页查询列表
	* @return
	*/
	@GetMapping(value = "/queryListBySearch", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result queryListBySearch(@ModelAttribute A1templatecolumnDto a1templatecolumnDto,
                                    @ModelAttribute PageDto pageDto){
		Result<A1templatecolumnDto> result = new Result<>();
		try {
			//与pageDto中的sort冲突
			a1templatecolumnDto.setSort(null);

			//查询参数
			//分页/排序查询条件
			//默认主键desc
			pageDto.configDefaultValue();

			//查询条件
			//keyRandom 转化 a1templateTableId
			if (!StringUtils.isNull(a1templatecolumnDto.getKeyRandom())) {
				Result<A1templatetableDto> result1 = templatetableService.selectByKeyRandom(a1templatecolumnDto.getKeyRandom());
				a1templatecolumnDto.setA1templatetableId(result1.getObj().getId());
			}

			//调用远程服务
			result =  a1templatecolumnService.pagination(a1templatecolumnDto,pageDto);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault(e.getLocalizedMessage());
		}
		return result;
    }

	@ApiOperation("")
	@GetMapping(value = "/selectById")
	@ResponseBody
	public Result selectById(Long id){
		Result<A1templatecolumnDto> result = new Result<>();
		try {

			//调用远程服务
			result =  a1templatecolumnService.selectById(id);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault(e.getLocalizedMessage());
		}
		return result;
	}


    /**
    * 伪删除数据
    * @return
    */
    @PostMapping(value = "/removeData", produces = "application/json; charset=utf-8")
    @ResponseBody
	public Result removeData(String id, HttpServletRequest request){
		Result<A1templatecolumnDto> result = new Result();
		try {
			HttpSession session = request.getSession();

			//参数
			String adminMan = WebUtils.getUsername(session);//加入管理员

			//调用远程服务
			result =  a1templatecolumnService.deleteById(Long.valueOf(id),adminMan);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}
		return result;
    }


    /**
    * 编辑数据
    * @return
    */
    @PostMapping(value = "/editData", produces = "application/json; charset=utf-8")
    @ResponseBody
    public Result editData(@RequestBody A1templatecolumnDto a1templatecolumnDto,
                           HttpServletRequest request, HttpServletResponse response){
    	Result<A1templatecolumnDto> result = new Result();
        try {
			HttpSession session = request.getSession();

			//参数
			String adminMan = WebUtils.getUsername(session);//加入管理员
			String userId = WebUtils.getUserId(session);
			a1templatecolumnDto.setModifymanId(userId);
			a1templatecolumnDto.setModifyman(adminMan);
			a1templatecolumnDto.setCompanyId(WebUtils.getCompanyId(session));

			//调用远程服务
			result =  a1templatecolumnService.editData(a1templatecolumnDto);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}

    /**
    * 编辑数据
    * @return
    */
    @PostMapping(value = "/editDataList", produces = "application/json; charset=utf-8")
    @ResponseBody
    public Result editDataList(@RequestBody List<A1templatecolumnDto> a1templatecolumnDtoList,
                               HttpServletRequest request, HttpServletResponse response){
    	Result result = new Result();
        try {
			HttpSession session = request.getSession();

			//调用远程服务
			result =  a1templatecolumnService.editDataList(a1templatecolumnDtoList);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}


	/**
	 * 更新列宽度
	 * @return
	 */
	@PostMapping(value = "/updateTmptColumn", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result updateTmptColumn(@RequestBody A1templatecolumnDto a1templatecolumnDto){
		Result<A1templatetableDto> result = new Result();
		try {
			return a1templatecolumnService.updateTmptColumn(a1templatecolumnDto, a1templatecolumnDto.getKeyRandom());
		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}

	/**
	 * 批量更新排序sort
	 * @return
	 */
	@PostMapping(value = "/editColumnSortByIds", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result editColumnSortByIds(@RequestBody List<Long> ids){
		Result<A1templatetableDto> result = new Result();
		try {
			return a1templatecolumnService.editColumnSortByIds(ids);
		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}

		return result;
	}

	/**
	* 添加数据
	* @return
	*/
	@PostMapping(value = "/addData", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Result addData(@RequestBody A1templatecolumnDto a1templatecolumnDto,
                          HttpServletRequest request, HttpServletResponse response){
		Result<A1templatecolumnDto> result = new Result();
		try {
			HttpSession session = request.getSession();

			//插入参数
			String adminMan = WebUtils.getUsername(session);//加入管理员
			String userId = WebUtils.getUserId(session);
			a1templatecolumnDto.setCreatemanId(userId);
			a1templatecolumnDto.setCreateman(adminMan);
			a1templatecolumnDto.setCompanyId(WebUtils.getCompanyId(session));

			//调用远程服务
			result = a1templatecolumnService.addData(a1templatecolumnDto);

		} catch (Exception e) {
			e.printStackTrace();
			result.fault("error:系统出错:"+e.getLocalizedMessage());
		}
		return result;
	}



	/**
		* 页面上查询列的值，动态加载，sql语句读数据库配置而来
		* when column.dataResource==2
		*/
	@GetMapping(value = "/select4a1templatetableId_condition_access", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String select4a1templatetableId_condition_access(){
		Map<String,Object> mapReturn = new HashMap<String,Object>();
		List<Map> list = a1templatecolumnService.select4a1templatetableId();
		Gson gson = new Gson();

		//查询条件中动态数据
		List<Map> listCopy = new ArrayList<Map>();
		listCopy.addAll(list);
		Map map = new HashMap();
		map.put("keyName","");
		map.put("valueName","全部");
		listCopy.add(0, map);

		return gson.toJson(listCopy);
	}

	/**
		* 页面上查询列的值，动态加载，sql语句读数据库配置而来
		* when column.dataResource==2
		*/
	@GetMapping(value = "/select4a1templatetableId_add_access", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String select4a1templatetableId_add_access(){
		Map<String,Object> mapReturn = new HashMap<String,Object>();
		List<Map> list = a1templatecolumnService.select4a1templatetableId();

		//添加编辑Dialog中，动态取数据（optionsql字段处理）
		JSONArray jsonArrayProperty = new JSONArray();
		for(Map map1: list){
			JSONObject jo = new JSONObject();
			jo.put("keyName",map1.get("keyName"));
			jo.put("valueName",map1.get("valueName"));
			jsonArrayProperty.put(jo);
		}

		return jsonArrayProperty.toString();
	}

}
